using Catalog.Application.CatalogItems.Interfaces;
using Catalog.Application.CatalogItems.Queries;
using Catalog.Application.Contracts.DTOs;
using MediatR;

namespace Catalog.Application.CatalogItems.QueryHandlers;

public class SearchCatalogItemQueryHandler : IRequestHandler<SearchCatalogItemQuery, List<CatalogItemProfileDto>>
{
    private readonly ICatalogItemReadService _catalogItemReadService;

    public SearchCatalogItemQueryHandler(ICatalogItemReadService catalogItemReadService)
    {
        _catalogItemReadService = catalogItemReadService;
    }

    public async Task<List<CatalogItemProfileDto>> Handle(SearchCatalogItemQuery request, CancellationToken cancellationToken)
    {
        var catalogItems = await _catalogItemReadService.GetListAsync(request.nameFilter, cancellationToken);
        return catalogItems.Select(item => new CatalogItemProfileDto
        {
            Id = item.Id,
            Name = item.Name,
            Description = item.Description,
            Price = item.Price,
            BrandId = item.BrandId,
            CategoryId = item.CategoryId
        }).ToList();
    }
}